package com.ecp.commons.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.ecp.ws.util.PropertiesFileUtil;

public final class RedisUtil {
	//可用连接实例的最大数目，默认值为8；
	//如果赋值为-1，则表示不限制；如果pool已经分配了maxActive个jedis实例，则此时pool的状态为exhausted(耗尽)。
	static int					maxActive;
	//控制一个pool最多有多少个状态为idle(空闲的)的jedis实例，默认值也是8。
	static int					maxIdle;
	//等待可用连接的最大时间，单位毫秒，默认值为-1，表示永不超时。如果超过等待时间，则直接抛出JedisConnectionException；
	static long					maxWait;
	//在borrow一个jedis实例时，是否提前进行validate操作；如果为true，则得到的jedis实例均是可用的;
	static boolean				testOnBorrow;
	static boolean				testOnReturn;
	static int					timeOut;
	//Redis服务器IP
	static String				ip;
	//Redis的端口号
	static int					port;
	//超过最大连接数时，新建连接
	static byte					whenExhaustedAction	= 2;

	static {
		port = Integer.parseInt(new PropertiesFileUtil().getProperties("config.properties",
				"redis.port"));
		ip = new PropertiesFileUtil().getProperties("config.properties", "redis.ip");
		maxActive = Integer.parseInt(new PropertiesFileUtil().getProperties("config.properties",
				"redis.pool.maxActive"));
		maxIdle = Integer.parseInt(new PropertiesFileUtil().getProperties("config.properties",
				"redis.pool.maxIdle"));
		maxWait = Long.valueOf(new PropertiesFileUtil().getProperties("config.properties",
				"redis.pool.maxWait"));
		testOnBorrow = Boolean.valueOf(new PropertiesFileUtil().getProperties("config.properties",
				"redis.pool.testOnBorrow"));
		testOnReturn = Boolean.valueOf(new PropertiesFileUtil().getProperties("config.properties",
				"redis.pool.testOnReturn"));
		timeOut = Integer.parseInt(new PropertiesFileUtil().getProperties("config.properties",
				"redis.pool.timeOut"));
	}

	private static JedisPool	jedisPool			= null;

	/**
	 * 初始化Redis连接池
	 */
	static {
		try {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxActive(maxActive);
			config.setMaxIdle(maxIdle);
			config.setMaxWait(maxWait);
			config.setTestOnBorrow(testOnBorrow);

			//连接池满后自动增加连接
			//config.setWhenExhaustedAction(whenExhaustedAction);
			//连接空闲的最小时间，达到此值后空闲链接将会被移除
			//config.setSoftMinEvictableIdleTimeMillis(30000);
			//config.setMinEvictableIdleTimeMillis(30000);
			//config.setTimeBetweenEvictionRunsMillis(30000);

			jedisPool = new JedisPool(config, ip, port, timeOut);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取Jedis实例
	 * @return
	 */
	public synchronized static Jedis getJedis() {
		try {
			if (jedisPool != null) {
				Jedis resource = jedisPool.getResource();
				return resource;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 释放jedis资源
	 * @param jedis
	 */
	public static void returnResource(final Jedis jedis) {
		if (jedis != null) {
			jedisPool.returnResource(jedis);
		}
	}

	/**
	 * 释放失败的jedis资源
	 * @param jedis
	 */
	public static void returnBrokenResource(final Jedis jedis) {
		if (jedis != null) {
			jedisPool.returnBrokenResource(jedis);
		}
	}
}
